package com.java2007.zhangyu.restaurant.controller.backend;

import com.java2007.zhangyu.restaurant.constant.ServletConstant;
import com.java2007.zhangyu.restaurant.controller.BaseServlet;
import com.java2007.zhangyu.restaurant.pojo.Admin;
import com.java2007.zhangyu.restaurant.service.AdminService;
import com.java2007.zhangyu.restaurant.service.impl.AdminServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 管理员控制器
 * @author zy
 * @Version
 * @date 2021/01/26
 */
@WebServlet("/admin")
public class AdminController extends BaseServlet {

    private AdminService adminService = new AdminServiceImpl();

    /**
     * 登录
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            //获取请求参数，用户名和密码
            String adminName = request.getParameter("adminName");
            String password = request.getParameter("password");

            Admin admin = adminService.login(adminName, password);

            request.getSession().setAttribute("loginAdmin", admin);

            return ServletConstant.PRE_REDIRECT + "/backend/index.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("loginFailMsg", e.getMessage());
            return ServletConstant.PRE_FORWARD + "/backend/login.jsp";
        }
    }
}
